package com.example.xianzhi.controller;

import cn.hutool.json.JSONUtil;
import com.example.xianzhi.entity.TbType;
import com.example.xianzhi.service.TypeService;
import com.example.xianzhi.service.impl.TypeServiceImpl;
import org.lanqiao.mvc.entity.ModelAndView;
import org.lanqiao.mvc.entity.YockMvcAnnotation;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@YockMvcAnnotation.Controller("/type")
public class TypeController {

    //分类业务层
    private TypeService type = new TypeServiceImpl();


    /**
     * 跳转分类页面
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @YockMvcAnnotation.RequestMapping("/toType.do")
    @YockMvcAnnotation.ResponseDispatch("/WEB-INF/view/typemanager.jsp")
    public ModelAndView toType(
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception{
        ModelAndView model = new ModelAndView();

        return model;
    }



    /**
     * 查询所有分类
     * @param request
     * @param response
     * @return
     */
    @YockMvcAnnotation.RequestMapping("/selectType.do")
    @YockMvcAnnotation.ResponseBody
    public String selectReleaseorder(
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        //告诉前端返回来的是一个json数据
        response.setContentType("application/json;charset=utf-8");
        // 保存分类信息的集合
        Map<String,Object> map = new HashMap<>();
        //查询所有分类
        List<TbType> tbTypes = type.selectType();

        map.put("code",200);
        map.put("data",tbTypes);


        String jsonStr = JSONUtil.toJsonStr(map);

        return jsonStr;
    }


    /**
     * 添加分类名称
     * @param request
     * @param response
     * @throws Exception
     */
    @YockMvcAnnotation.RequestMapping("/addtype.do")	//配置二级路径
    @YockMvcAnnotation.ResponseVoid
    public void addtype(
            HttpServletRequest request,
            HttpServletResponse response,
            @YockMvcAnnotation.RequestParam(name = "name") String name
    ) throws Exception{

        type.insertType(name);


    }


    /**
     * 删除分类
     * @param request
     * @param response
     * @throws Exception
     */
    @YockMvcAnnotation.RequestMapping("/deletetype.do")	//配置二级路径
    @YockMvcAnnotation.ResponseVoid
    public void deletetype(
            HttpServletRequest request,
            HttpServletResponse response,
            @YockMvcAnnotation.RequestParam(name = "type_id") int  type_id
    ) throws Exception{

        type.deleteType(type_id);

    }

}
